APP = _serve = undefined

config = require('../config/fc')
PREFIX_LEN = config.HTTP_PREFIX.length+config.SERVICE_FUNCTION.length



serve = (req, res)->
    res.constructor.prototype.removeHeader = ->
    res.constructor.prototype.end = (body)->
        @send(body)
    serve = _serve
    _serve(req, res)

module.exports = {
    koa: (req,res,context) ->
        req.path = req.path.slice(PREFIX_LEN)
        req.url = req.url.slice(PREFIX_LEN)
        req.headers['x-forwarded-for'] = req.clientIP
        serve(req, res)
        return

    init: (context, callback)->
        require('./koa') (app) ->
            APP = app
            app.proxy = true
            _serve = app.callback()
            callback(null, '')
}
